package com.oa.jixiao.controller;



import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.oa.jixiao.biz.AssessmentBiz;
import com.oa.jixiao.entity.Assessment;
import com.oa.login.biz.EmpBiz;
import com.oa.login.entity.Emp;

@Controller
@RequestMapping("/assess")
public class AssessmentController {
	
	@Resource
	private AssessmentBiz assBiz;
	
	@Resource
	private EmpBiz empBiz;
	
	@RequestMapping("/names")
	public String listNames(Model model,String name,String number) throws Exception {
		String str = new String(name.getBytes("iso8859-1"),"utf-8");
		Assessment assess=new Assessment();
		assess.setAssNumber(number);
		assess.setMoth(LocalDateTime.now().getMonthValue());
		int rows=assBiz.findByNameAndNumber(assess);
		if(rows>0){
			model.addAttribute("msg", "您已投票，无需再投，谢谢！");
		}else{
			List<Emp> list=assBiz.findAll(str);
			model.addAttribute("listNames",list);
		}
		return "/jixiao/assess";
	}
	
	@ResponseBody
	@RequestMapping("/add")
	public int add(String name,String score,int month,String number){
		String[] names=name.split(",");
		String[] scores=score.split(",");
		List<Assessment> list=new ArrayList<>();
		for(int i=0;i<names.length;i++){
			Assessment ass=new Assessment();
			ass.setName(names[i]);
			ass.setScore(scores[i]);
			ass.setMoth(month);
			ass.setAssNumber(number);
			list.add(ass);
		}
		int rows=0;
		for(Assessment ass:list){
			assBiz.save(ass);
			rows++;
		}
		return rows;
	}
	
	@RequestMapping("/doScore")
	public String turn(){
		return "/jixiao/score";
	}
	
	
	@ResponseBody
	@RequestMapping("/score")
	public List<Assessment> show(int month){
//		String[] names = empBiz.findNames();
		List<Assessment> list=assBiz.totalScore(month);
		
		return list;
	}
}	
